package com.sgb.capital.view.pop;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.sgb.capital.R;
import com.sgb.capital.utils.KeyboardUtils;
import com.sgb.capital.utils.Utils;
import com.sgb.capital.utils.WithdrawalTimerUtils;
import com.sgb.capital.view.widget.LastInputEditText;
import com.sgb.capital.view.widget.VerificationInputView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import razerdp.basepopup.BasePopupWindow;

/**
 * 作者:张磊
 * 日期:2021/4/16 0016
 * 说明:提现发送验证码
 */
public class NewWithdrawPop extends BasePopupWindow {

    private WithdrawalTimerUtils mWithdrawalTimerUtils;
    private TextView mTvBankCard;
    private TextView mTvBankAccountName;
    private TextView mTvPrice;
    public String mPagreementId;
    private TextView mTvTime;
    private VerificationInputView mVerificationInputView;
    private TextView mTvPhone;
    private final DialogButtonListener mListener;
    private String mPhone;
    private View mTvSure;
    private String mPrice;
    private int mDoneNum;
    private String mCode;
    public TextView mTvTip;
    private TextView mTvCommission;
    private boolean mIsUser;

    public NewWithdrawPop(Context context, DialogButtonListener listener) {
        super(context);
        mListener = listener;
    }

    @Override
    public View onCreateContentView() {
        View v = createPopupById(R.layout.newwithdraw_pop);
        // 银行卡账号
        mTvBankCard = v.findViewById(R.id.tv_bankCard);
        // 银行名称
        mTvBankAccountName = v.findViewById(R.id.tv_bankAccountName);
        // 提现金额
        mTvPrice = v.findViewById(R.id.tv_price);
        // 倒计时
        mTvTime = v.findViewById(R.id.tv_time);
        // 验证码
        mVerificationInputView = v.findViewById(R.id.verification_input_view);
        // 手机号
        mTvPhone = v.findViewById(R.id.tv_phone);
        // 提示
        mTvTip = v.findViewById(R.id.tv_tip);
        // 手续费
        mTvCommission = v.findViewById(R.id.tv_commission);
        // 取消
        v.findViewById(R.id.tv_cancel).setOnClickListener(v1 -> dismiss());
        // 确定
        mTvSure = v.findViewById(R.id.tv_sure);
        initListener();
        return v;
    }

    public void initListener() {
        mVerificationInputView.setListener(new VerificationInputView.Listener() {
            @Override
            public void onChange(String[] strings, List<LastInputEditText> editTextList) {
                mDoneNum = 0;
                for (int m = 0; m < strings.length; m++) {
                    if (!strings[m].isEmpty()) {
                        mDoneNum++;
                    }
                }
                if (mDoneNum == 6) {
                    mTvPrice.setFocusable(true);
                    mTvPrice.requestFocus();
                    mTvPrice.setFocusableInTouchMode(true);
                    KeyboardUtils.hideSoftInput(mTvPrice);
                    mTvTip.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onComplete(String string) {
                mCode = string;
            }
        });
        mTvTime.setOnClickListener(v -> {
            if (mTvTime.getText().toString().contains("重新")) {
                mVerificationInputView.clear();
                mListener.applyCode(mPhone);
            }
        });
        mTvSure.setOnClickListener(v -> {
            if (Utils.isEmpty(mCode)) {
                mTvTip.setText("请输入验证码");
                mTvTip.setTextColor(Utils.getColor(R.color.color_FF2928));
                mTvTip.setVisibility(View.VISIBLE);
                return;
            }
            Map map = new HashMap();
            map.put("amount", mPrice);
            map.put("mobile", mPhone);
            map.put("code", mCode);
            map.put("pagreementId", mPagreementId);
            mListener.applyWithdrawal(map);
        });

    }


    public void setData(String pagreementId, String bankCard, String bankAccountName, String price, String mobileId, boolean isUser) {
        mPagreementId = pagreementId;
        mIsUser = isUser;
        mTvCommission.setText(mIsUser ? "手续费：¥0.40/笔" : "手续费：¥2.00/笔");
        mPhone = mobileId;
        mPrice = price;
        mTvBankCard.setText(bankCard);
        mTvBankAccountName.setText(bankAccountName);
        mTvPrice.setText(Utils.getDecimalFormat(Double.parseDouble(price)));
        mTvPhone.setText("验证码已发送至您手机" + mobileId.substring(0, 3) + "****" + mobileId.substring(mobileId.length() - 4));
    }


    public void show() {
        mVerificationInputView.clear();
        mCode = null;
        if (mWithdrawalTimerUtils != null) {
            mWithdrawalTimerUtils.onFinish();
        }
        showPopupWindow();
    }

    public void setStartTimer() {
        if (mWithdrawalTimerUtils != null) {
            mWithdrawalTimerUtils.cancel();
        }
        mWithdrawalTimerUtils = new WithdrawalTimerUtils(mTvTime, 60000, 1000); //倒计时1分钟
        mWithdrawalTimerUtils.start();
    }

    public interface DialogButtonListener {
        void applyCode(String phone);

        void applyWithdrawal(Map map);
    }
}